package com.yvan.leto.boot;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

/**
 * Created by xiaoEight on 2018/6/14.
 */
@Slf4j
public class LetoConfigurableInitializer  implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private final static String SPRING_BOOT_NAME = "applicationConfig:[leto-spring-root]";

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        LetoSpringContext.setBaseApplicationContext(applicationContext);
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        if(LetoSpringContext.getBootMap()!=null && !LetoSpringContext.getBootMap().isEmpty()){
            propertySources.addFirst(new MapPropertySource(SPRING_BOOT_NAME, LetoSpringContext.getBootMap()));
        }
    }
}
